import { Component, OnInit, Injector } from '@angular/core';
import { HttpService } from "../core/service/http.service";
import { v4 as uuid } from 'uuid';
import { AuthService } from '..';
import { NgZorroPageBaseComponent } from '../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { SignalRService } from '../core/service/singnalr-service';
import { ActivatedRoute, Router, NavigationExtras } from '@angular/router';
import { map } from 'rxjs/operators';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
@Component({
  selector: 'app-room',
  templateUrl: './room.component.html',
  styleUrls: ['./room.component.scss'],
  providers: [HttpService]
})
export class RoomComponent implements OnInit {
  list;
  // roomAdmin = { studentName: "房主", id: 0, roleName: "申请人", userPhoto: "" };
  recordId = 0;
  studentId = 0;
  roomAdminId = 0;
  constructor(public http: HttpService,
    private route: ActivatedRoute,
    private router: Router,
    private modal: NzModalService,
    private signalrService: SignalRService,
    private message: NzMessageService,
    private auth: AuthService
  ) {
    // this.getRoleList(25);
    this.studentId = this.auth.token.userid;
    this.signalrService.messageReceived.subscribe(e => {
      if (this.studentId.toString() !== e.personId.toString() && this.recordId.toString() === this.recordId.toString()) {
        this.getRoleList();
      }
    });
    this.signalrService.startDrill.subscribe(e => {
      const navigationExtras: NavigationExtras = {
        queryParams: { 'id': this.recordId }
      };
      this.router.navigate(['/mydrillcontent'], navigationExtras);
    });
  }
  ngOnInit() {
    this.route.queryParamMap
      .pipe(map(params => params.get('id') || null))
      .subscribe(async e => {
        this.recordId = Number(e);
        if (!this.recordId) {
          this.message.error('无法确认房间信息!');
        } else {
          const result = await this.http.get<any>(`DrillRecord/GetRecordToById?RecordId=${this.recordId}`);
          if (result) {
            this.roomAdminId = result.creatorRoleId;
            this.signalrService.JoinRoom(this.recordId.toString()).then(joined => { this.getRoleList(); });
          } else {
            this.router.navigate(['./hall']);
          }
        }
      });
  }

  // async cancelSelected() {
  //   const result = await this.http.post<any>('DrillRecord/CancelRecordFlowStudent',
  //     { drillRecordId: this.recordId, studentInfoId: this.studentId });
  //   this.signalrService.SelectRole(this.recordId.toString(), this.studentId.toString());
  // }

  async selectRole(roleId: number) {
    // if (parseInt(this.studentId.toString(), 10) !== parseInt(this.roomAdminId.toString(), 10)) {
      const result = await this.http.post<any>('DrillRecord/UpdateRecordFlowStudent', {
        drillRecordId: this.recordId,
        studentInfoId: this.studentId,
        roleId: roleId
      });
      this.signalrService.SelectRole(this.recordId.toString(), this.studentId.toString());
      this.getRoleList();
    // } else {
    //   this.message.error('房主不能切换角色');
    // }

  }

  async getRoleList() {
    const findList = await this.http.get<Array<any>>(`DrillRecord/GetAllRoleOutput?recordId=${this.recordId}`);
    this.list = findList;
    // this.roomAdmin = findList.filter(e => e.personId === this.roomAdminId)[0];
  }

  reload() {
    window.location.reload();
  }

  // 开始演练
  async startDrill() {
    this.modal.confirm({
      nzTitle: '是否要开始演练?',
      nzContent: '点击确定后，此演练的信息将不能更改。',
      nzOnOk: async () => {
        const startpar = { Id: this.recordId };
        const result = await this.http.post<any>('DrillRecord/StartDrillRecord', startpar);
        this.signalrService.Start(this.recordId.toString());
      },
      nzOnCancel: () => {

      }
    });
  }
}



